package com.shameit.goodqrinfomanager.ui.activity.offline;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import com.chad.library.BR;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.shameit.goodqrinfomanager.AppViewModelFactory;
import com.shameit.goodqrinfomanager.BaseMVVMActivity;
import com.shameit.goodqrinfomanager.R;
import com.shameit.goodqrinfomanager.adapter.OfflineListAdapter;
import com.shameit.goodqrinfomanager.bean.enity.OfflinListBean;
import com.shameit.goodqrinfomanager.databinding.ActivityOfflineManagementBinding;
import com.shameit.goodqrinfomanager.ui.activity.barcode.BarCodeManagementActivity;
import com.shameit.goodqrinfomanager.ui.activity.barcode.BarCodeViewModel;

import org.litepal.LitePal;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseViewModel;

public class OfflineManagementActivity extends BaseMVVMActivity<ActivityOfflineManagementBinding,OfflineManagementViewModel> {
    public static final SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolBarBack(binding.toolbar);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_offline_management;
    }

    @Override
    public int initVariableId() {
        return BR.viewmodel;
    }

    @Override
    public OfflineManagementViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(OfflineManagementViewModel.class);
    }

    @Override
    protected String setTitle() {
        return "离线管理";
    }

    @Override
    protected void initView() {
        binding.offlineList.setLayoutManager(new LinearLayoutManager(this));
        binding.offlineList.setAdapter(viewModel.adapter=new OfflineListAdapter(R.layout.offline_layout_item));
    }

    @Override
    protected void loadData() {
        String[] arr = viewModel.loadCangkuItem();
        ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.support_simple_spinner_dropdown_item, arr);
        binding.spCkbh.setAdapter(arrayAdapter);

        binding.spCkbh.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                binding.spCwbh.setAdapter(null);
                String thisCangku = arr[position];
                binding.spCwbh.setAdapter(new ArrayAdapter(OfflineManagementActivity.this, R.layout.support_simple_spinner_dropdown_item, viewModel.loadCangweiItem(thisCangku)));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
//        List<OfflinListBean> list = new ArrayList<OfflinListBean>();
//        for (int i = 0; i < 10; i++) {
//            OfflinListBean bean = new OfflinListBean();
//            bean.setDanCode(String.valueOf(i));
//            bean.setMaNumber(String.valueOf(i));
//            bean.setTime(dateTimeFormat.format(new Date()));
//            bean.setUpState(i%2==0?true:false);
//            bean.setXiangNumber(String.valueOf(i));
//            list.add(bean);
//        }
        List<OfflinListBean> list = viewModel.getListBean();
        viewModel.adapter.setNewInstance(list);
        viewModel.adapter.setData(list);
    }
}